﻿using Direct.IDAL.User.Jurisdiction;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DirectSell.Model.User;

namespace DirectSell.BLL.User.Jurisdiction
{
    /// <summary>
    /// 修改密码服务层
    /// </summary>
    public  class ModifyPasswordSevice:UserService
    {
        #region 日志记录--郭文昊--2018年10月31日14:40:29
        /// <summary>
        /// 日志记录文件夹
        /// </summary>
        protected override string LogFile => "修改密码";
        #endregion

        #region 初始化数据--郭文昊--2018年10月31日14:43:14
        /// <summary>
        /// 修改密码数据访问接口
        /// </summary>
        protected IModifyPasswordRepository _modifyPassword { get; set; }
        public ModifyPasswordSevice(IModifyPasswordRepository modifyPassword)
        {
            this._modifyPassword = modifyPassword;
        }
        #endregion

        #region 修改密码操作--郭文昊--2018年10月31日14:57:43
        /// <summary>
        /// 修改密码操作
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="param"></param>
        /// <returns></returns>
        protected override async Task<UserRespCode> OperationAsync<T>(T param)
        {
            var model = param as ModifyPassword;

            if (model == null)
            {
                return UserRespCode.传入参数不正确;
            }
            //判断原始密码
            if(await CheckPassword(model) == false)
            {
                return UserRespCode.初始密码输入错误;
            }
            //记录日志
            await LogAsync(model);
            //修改密码
            if (!ModifyPassword(model))
            {
                return UserRespCode.失败;
            }
            return UserRespCode.成功;
        }
        #endregion

        #region 判断原始密码--郭文昊--2018年10月31日14:59:45
        /// <summary>
        /// 判断原始密码是否正确
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        protected async Task<bool> CheckPassword(ModifyPassword _model)
        {
            if(_model.Type==PasswordType.一级密码)
            {
                return await _modifyPassword.CheckPassword(_model.UserCode, _model.OldPassword);
            }
            if (_model.Type == PasswordType.二级密码)
            {
               return  await _modifyPassword.CheckSecondPassword(_model.UserCode, _model.OldPassword);
            }
            return false;
        }
        #endregion

        #region 修改密码--郭文昊--2018年10月31日15:06:09
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="_model"></param>
        /// <returns></returns>
        protected  bool ModifyPassword(ModifyPassword _model)
        {
            if (_model.Type == PasswordType.一级密码)
            {
                return  _modifyPassword.ModifyPassword(_model.NewPassword, _model.UserCode);
            }
            if (_model.Type == PasswordType.二级密码)
            {
                return  _modifyPassword.ModifySecondPassword(_model.NewPassword, _model.UserCode);
            }
            return false;
        }
        #endregion

    }
}
